﻿@{
   // ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";

    var lstNhomDonVi = ViewBag.lstNhomDonVi;
    var lstDonVi = ViewBag.lstDonVi;
}
<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            @*<a href="#">Quản lý người dùng Quản trị hệ thống các đơn vị</a>*@
            <a href="#">@ViewBag.CurrentMenuName</a>
        </li>
    </ul>
</div>
<div class="row">
    <div class="col-md-2 col-sm-2" style="">@Html.Label("Nhóm đơn vị:")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
        @Html.DropDownList("nhom_don_vi", lstNhomDonVi as List<SelectListItem>, null, new { @class = "form-control input-sm" })
    </div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Đơn vị:")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
        @Html.DropDownList("don_vi", lstDonVi as List<SelectListItem>, null, new { @class = "form-control input-sm" })
    </div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Họ tên: ", new { id = "lblName", @class = "" })</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("ho_ten", "", new { @class = "form-control input-sm", style = "", placeholder = "Họ tên", maxlength = 250 })</div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Tên đăng nhập: ")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("tai_khoan", "", new { @class = "form-control input-sm", style = "", placeholder = "Tài khoản", maxlength = 50 })</div>

    <div class="col-md-2 col-sm-2" style="">@Html.Label("Điện thoại: ")</div>
    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("dien_thoai", "", new { @class = "form-control input-sm", style = "", placeholder = "Điện thoại", maxlength = 50 })</div>

    @*<div class="col-md-2 col-sm-2" style="">@Html.Label("Trạng thái")</div>
        <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
        @Html.DropDownList("trang_thai", slState , null, new { @class = "form-control input-sm" })</div>*@

    <div class="col-md-6 col-sm-6">
        <div class="pull-right">
            <button id="btnSearch" class="btn btn-sm btn-default"><i class="fa fa-search"></i> Tìm kiếm</button>
            <button id="btnAdd" class="btn btn-sm btn-default" style="cursor:pointer;"><i class="fa fa-plus-square-o"></i> Tạo mới</button>
            <div id="divPrintEnum" style="display:none;"></div>
        </div>
    </div>
</div>

<div id="gridContent" align="center" style="padding:10px;">
    @if (Model.Data != null && Model.Data.Count > 0)
    {
        Html.RenderPartial("_NguoiDungAdminList");
    }
    else
    {
        <h4>Không tìm thấy kết quả nào phù hợp</h4>
    }
</div>


<script type="text/javascript">
    $(document).ready(function () {

        $('#btnSearch').bind('click', function (event) {
            btnSearchClick();
        });

        $('#btnAdd').bind('click', function (event) {
            functionCreateOrUpdate("", "Thêm mới", "Tạo mới");
        });

        $('#input-state').change(function () {
            OnCriteria(0);
        });

        $("#input-code", "#input-name").on("keypress", function (event) {
            if (event.which == 13) {
                OnCriteria(0);
            }
        });
        headers['VerificationToken'] = '@GetAntiForgeryToken()';
    });

    var headers = {};

    function btnSearchClick() {
        OnCriteria(0);
    }

    function nhomdonviEdit(id) {
        functionCreateOrUpdate(id, "Cập nhật", "Cập nhật");
    }

    $('#nhom_don_vi').change(function () {
        var nhomid = $('#nhom_don_vi').val();
        $.ajax({
            dataType: "json",
            type: "POST",
            url: '/NguoiDungAdmin/GetDonViByNhom',
            contentType: "application/json",
            data: JSON.stringify({strNhomDonViID: nhomid }),
            success: function (data) {
                var result = '';
                for (var i = 0; i < data.length; i++) {
                    result += '<option value="' + data[i].Value + '">' + data[i].Text + '</option>';
                }
                $("#don_vi").html(result);
                //OnCriteria(0);
            }
        });
    });

    function functionCreateOrUpdate(id, titleForm, buttonTile) {
        $("#divLoading").show();
        var ajax = $.ajax({
            url: '/NguoiDungAdmin/CreatePopupUpdate',
            type: 'POST',
            data: { id: id },
            //contentType: "appliction/json",
            success: function (data) {
                $("#divLoading").hide();
                var box = bootbox.dialog({
                    title: titleForm,
                    message: data,
                    onEscape: function () {
                        box.modal('hide');
                    },
                    buttons: {
                        success: {
                            label: "<i class='fa fa-plus-square-o'></i> " + buttonTile,
                            className: "btn btn-md btn-success",
                            callback: function () {
                                var form = $('#frmNguoiDungAdmin');
                                if (!form.valid()) {
                                    return false;
                                }
                                $("#divLoading").show();
                                //alert("headers");
                                $.ajax({
                                    url: '/NguoiDungAdmin/ActionSave',
                                    headers: headers,
                                    data: {
                                        NGUOI_DUNG_ID: $("#NGUOI_DUNG_ID").val(),
                                        DON_VI_ID: $("#DON_VI_ID").val(),
                                        HO_VA_TEN: $("#HO_VA_TEN").val(),
                                        TAI_KHOAN: $("#TAI_KHOAN").val(),
                                        DIEN_THOAI: $("#DIEN_THOAI").val()
                                    },
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (response) {
                                        if (response != null && response.status == 200) {
                                            Layout.toastr("",response.result);
                                            OnCriteria(currentPageIndex);
                                            result = true;
                                        } else {
                                            Layout.toastr("", response.result);
                                        }
                                        $("#divLoading").hide();
                                    },
                                    error: function (errorThrown) {//function(request, textStatus, errorThrown)  {
                                        alert(JSON.stringify(errorThrown));
                                        //alert(request.getAllResponseHeaders());
                                        //alert(request.getResponseHeader('WWW-Authenticate'));
                                    }
                                });

                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-md btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            },
            complete: function () {
                setTimeout(function () {
                    $("#HO_VA_TEN").focus();
                }, 500);
            },
            error: function (ex) {
                alert(JSON.stringify(ex));
            }
        });
        return false;
    }

    var currentPageIndex;
    function OnCriteria(pageIndex) {
        currentPageIndex = pageIndex;
        var name = $('#ho_ten').val().trim();
        var account = $("#tai_khoan").val().trim();
        var phone = $("#dien_thoai").val().trim();
        var nhomdonvi = $('#nhom_don_vi').val().trim();
        var donvi = $('#don_vi').val().trim();
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/NguoiDungAdmin/ActionSearch',
            //contentType: "application/json",
            data: { NHOM_DON_VI_ID: nhomdonvi, DON_VI_ID: donvi, HO_VA_TEN: name, TAI_KHOAN: account, DIEN_THOAI: phone, pageIndex: pageIndex },
            success: function (response) {
                var $catlist = $('#gridContent');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }
</script>